import { defineStore } from 'pinia';
import { ref } from 'vue';
import { IUserInfo } from '../typings';

const initState = {
	avatar: '',
	student_id: null,
	user_name: '',
	city_name: '',
	county_name: '',
	school_name: null,
	grade_name: null,
	class_name: null,
	city_id: null,
	county_id: null,
	school_id: null,
	grade: null,
	class_id: null,
};

const tokenState = {
	token: '',
	session_key: '',
	openid: '',
	unionid: '',
};

export const useUserStore = defineStore(
	'user',
	() => {
		const userInfo = ref<IUserInfo>({ ...initState });
		const tokenInfo = ref<IUserInfo>({ ...tokenState });
        const uid = ref<Number>(0);
		
		const setUserInfo = (val: IUserInfo) => {
			userInfo.value = val || initState;
		};

		const setTokenInfo = (val: IUserInfo) => {
			tokenInfo.value = val || tokenState;
		};
     
	   const setUid = (val: Number) => {
	   	   uid.value = val; 
	   };

		const clearUserInfo = () => {
			userInfo.value = { ...initState };
			tokenInfo.value = { ...tokenState };
			uid.value = 0;
		};
		const reset = () => {
			userInfo.value = { ...initState };
			tokenInfo.value = { ...tokenState };
			uid.value = 0;
		};

		return {
			userInfo,
			tokenInfo,
			uid,
			setTokenInfo,
			setUserInfo,
			clearUserInfo,
			reset,
			setUid
		};
	},
	{
		persist: true,
	},
);
